# import time, threading
#
# # 新线程执行的代码:
# def loop():
#     print ('thread %s is running...' % threading.current_thread().name)
#     n = 0
#     while n < 5:
#         n = n + 1
#         print ('thread %s >>> %s' % (threading.current_thread().name, n))
#         time.sleep(1)
#     print ('thread %s ended.' % threading.current_thread().name)
#
# print ('thread %s is running...' % threading.current_thread().name)
# t = threading.Thread(target=loop, name='LoopThread')
# t.start()
# t.join()
# print ('thread %s ended.' % threading.current_thread().name)


# from time import ctime
# from time import sleep
# import threading
#
# sleepsecs = [4, 8]
#
# def loop(loopname, secs):
#     print ('start loop开始',loopname, ' at: ', ctime())
#     sleep(secs)
#     print ('end loop结束',loopname, ' at: ', ctime())
#
#
# if __name__ == '__main__':
#     # Thread对象列表
#     threads = []
#
#     for i in range(len(sleepsecs)):
#         print(i)
#         thread = threading.Thread(target=loop, args=(i, sleepsecs[i]))# 创建一个线程  需要线程去执行的方法名：target/ args: 线程执行方法接收的参数，该属性是一个元组，如果只有一个参数也需要在末尾加逗号。工鞥类似于thread中的锁（每个线程分配一个锁）for i in nloops:lock = thread.allocate()  lock.acquire()   locks.append(lock)
#         # print("打印{}".format(thread))
#         print("d打印：%s"%thread )
#         threads.append(thread)
#         print(threads)
#
#     print ('start main at :', ctime())
#     # 逐个启动线程
#     for i in range(len(sleepsecs)):
#         print(i)
#         threads[i].start()#开始执行，启动线程
#         # sleep(0.001)
#
#     for i in range(len(sleepsecs)):
#         threads[i].join()#直至启动的线程终止之前一直挂起，线程不会立即开始
#
#     print ('end main at ', ctime())

# import threading
# from time import sleep, ctime
#
# # 不再把4秒和2秒硬性的编码到不同的函数中，而是使用唯一的loop()函数，并把这些常量放进列表loops中
# loops = [4,1]
#
#
# def loop(nloop):
#     print('开始循环', nloop, 'at:', ctime())
#     # print("睡眠时间{}".format(nsec) )
#     sleep(0.001)
#     print('循环', nloop, '结束于：', ctime())
#
#
# def main():
#     print('程序开始于：', ctime())
#     threads = []
#     # nloops1= range(len(loops))
#     nloops2= range(8)
#
#     for i in nloops2:
#         # t = threading.Thread(target=loop, args=(i, loops[i]))  # 循环 实例化2个Thread类，传递函数及其参数，并将线程对象放入一个列表中
#         t = threading.Thread(target=loop,args=(i,))
#         print("i{}".format(i) )
#         threads.append(t)
#         # print(t)
#
#     for i in nloops2:
#         threads[i].start()  # 循环 开始线程
#         print("线程{}".format(i) )
#
#     for i in nloops2:
#         threads[i].join()  # 循环 join()方法可以让主线程等待所有的线程都执行完毕。
#
#     print('任务完成于：', ctime())
#
#
# if __name__ == '__main__':
#     main()


import threading

import time

def worker(number):

    print ("worker")

    time.sleep(number)

    return

for i in range(5):

    t = threading.Thread(target=worker,args=(i,))

    t.start()